package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import static android.content.Context.INPUT_METHOD_SERVICE;
public class LoginWithanim  extends AppCompatActivity implements TextWatcher {
    private EditText InputUserName;
    private EditText InputPassWord;
    private Button loginBtn;
    private Button logupBtn;
    private ImageView leftArm;
    private ImageView rightArm;
    private ImageView leftHand;
    private ImageView rightHand;
    public static final String sqlUserName="root";
    public static final String sqlPassWord="tzyTZY123";
    private TextView tip;
    public static String LoginUserName="";

    public static final String Url="jdbc:mysql://cdb-l1c6g0ne.bj.tencentcdb.com:10166/AndroidUser?user=root&password=tzyTZY123";
    public static final String Driver="com.mysql.jdbc.Driver";
    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet;
    public  boolean LoginVerify(String inputUserName,String inputPassWord) throws ClassNotFoundException {//登录成功为true，反之为false
        ArrayList<String> password = new ArrayList<>();
        try {
            //1,加载驱动
            Class.forName(Driver);
            //2.创建连接
            connection = DriverManager.getConnection(Url);
            String MySql = "select * from android_user where UserName=?";//?代表参数会在以后加
//            String MySql = "select * from android_user";
            preparedStatement = connection.prepareStatement(MySql);
            preparedStatement.setString(1, inputUserName);//代表第一个？放置这个参数
            resultSet =  preparedStatement.executeQuery();
            //遍历查询结果集
            password=new ArrayList<>();
            while(resultSet.next()){
                password.add(resultSet.getString("PassWord"));
            }
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
        System.out.println(password);
        if(password.size()==0){
            return false;
        }
        else if (password.get(0).equals(inputPassWord)){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_withanim);
        initViews();
    }

    public void initViews(){
        tip=(TextView)findViewById(R.id.tip);
        InputUserName=(EditText)findViewById(R.id.InputUserName);
        InputPassWord=(EditText)findViewById(R.id.InputPassWord);
        loginBtn = (Button)findViewById(R.id.AnimLogin);
        logupBtn=(Button)findViewById(R.id.AnimLogup);
        leftArm = findViewById(R.id.iv_left_arm);
        rightArm = findViewById(R.id.iv_right_arm);
        leftHand = findViewById(R.id.iv_left_hand);
        rightHand = findViewById(R.id.iv_right_hand);

        //监听内容改变 -> 控制按钮的点击状态
        InputUserName.addTextChangedListener(this);
        InputPassWord.addTextChangedListener(this);

        //监听EidtText的焦点变化 -> 控制是否需要捂住眼睛
        InputPassWord.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus == true){
                    //捂住眼睛
                    close();
                }else{
                    //打开
                    open();
                }
            }
        });

    }

    /**
     * 当有控件获得焦点focus 自动弹出键盘
     * 1. 点击软键盘的enter键 自动收回键盘
     * 2. 代码控制 InputMethodManager
     *    requestFocus
     *    showSoftInput:显示键盘 必须先让这个view成为焦点requestFocus
     *
     *    hideSoftInputFromWindow 隐藏键盘
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            //隐藏键盘
            //1.获取系统输入的管理器
            InputMethodManager inputManager =
                    (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

            //2.隐藏键盘
            inputManager.hideSoftInputFromWindow(InputUserName.getWindowToken(),0);

            //3.取消焦点
            View focusView = getCurrentFocus();
            if (focusView != null) {
                focusView.clearFocus(); //取消焦点
            }

            //getCurrentFocus().clearFocus();

            //focusView.requestFocus();//请求焦点
        }
        return true;
    }



    public void ClickLoginWithAnim(View view) throws ClassNotFoundException {//登录
        System.out.println(InputUserName.getText());
        System.out.println(InputPassWord.getText());
        Editable theUserName=InputUserName.getText();
        Editable thePassWord=InputPassWord.getText();
        new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    if(LoginVerify(theUserName.toString(),thePassWord.toString())){
                        LoginUserName=theUserName.toString();
                        startActivity(new Intent(LoginWithanim.this,myWelcomeActivity.class));
                    }
                    else {
                        tip.setText("PassWord Error");

                    }
                    System.out.println(LoginVerify(theUserName.toString(),thePassWord.toString()));
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
    public void ClickLogupWithAnim(View view){
        startActivity(new Intent(LoginWithanim.this,Logup.class));


    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        //判断两个输入框是否有内容
        if (InputUserName.getText().toString().length() > 0 &&
                InputPassWord.getText().toString().length() > 0){
            //按钮可以点击
            loginBtn.setEnabled(true);
        }else{
            //按钮不能点击
            loginBtn.setEnabled(false);
        }
    }

    public void close(){
        //左边
        RotateAnimation lAnim = new RotateAnimation(0,170,leftArm.getWidth(),0f);
        lAnim.setDuration(500);
        lAnim.setFillAfter(true);

        leftArm.startAnimation(lAnim);

        RotateAnimation rAnim = new RotateAnimation(0, -170,0f,0f);
        rAnim.setDuration(500);
        rAnim.setFillAfter(true);

        rightArm.startAnimation(rAnim);

        TranslateAnimation down = (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.hand_down_anim);
        leftHand.startAnimation(down);
        rightHand.startAnimation(down);
    }

    public void open(){
        //左边
        RotateAnimation lAnim = new RotateAnimation(170,0,leftArm.getWidth(),0f);
        lAnim.setDuration(500);
        lAnim.setFillAfter(true);

        leftArm.startAnimation(lAnim);

        RotateAnimation rAnim = new RotateAnimation(-170,0,0f,0f);
        rAnim.setDuration(500);
        rAnim.setFillAfter(true);

        rightArm.startAnimation(rAnim);

        TranslateAnimation up = (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.hand_up_anim);
        leftHand.startAnimation(up);
        rightHand.startAnimation(up);
    }

}
